package leetcode.每日一题;

import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2022/1/2 10:46
 * @description：https://leetcode-cn.com/problems/elimination-game/
 */
public class 消除游戏 {
    @Test
    public void test() {
        Solution solution = new Solution();
        System.out.println(solution.lastRemaining(24));
    }

    class Solution {
        public int lastRemaining(int n) {
            int a1 = 1, an = n;
            int k = 0, cnt = n, step = 1;
            while (cnt > 1) {
                // 正向
                if (k % 2 == 0) {
                    a1 += step;
                    an -= cnt % 2 == 0 ? 0 : step;
                } else {
                    a1 += cnt % 2 == 0 ? 0 : step;
                    an -= step;
                }
                step *= 2;
                cnt >>= 1;
                k++;
            }
            return a1;
        }
    }
}
